package com.itheima.service.impl;

import com.itheima.mapper.PeopleMapper;
import com.itheima.mapper.ProjectMapper;
import com.itheima.pojo.People;
import com.itheima.pojo.Project;
import com.itheima.pojo.Result;
import com.itheima.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private PeopleMapper peopleMapper;
    @Override
    public void addProject(Project project) {
        projectMapper.insert(project);
    }

    @Override
    public List<Project> getProjectList() {

        return projectMapper.getProjectList();
    }

    @Override
    public void changeKahuna(Integer id,String kahunaName) {
        People people = new People();
        people.setName(kahunaName);
        List<People> peopleList =  peopleMapper.searchPeople(people);
        projectMapper.changeKahuna(id,peopleList.get(0).getId());
    }

    @Override
    public Result deleteProject(Project project) {
        Project pro= new Project();
        pro.setId(project.getId());
        Project p =projectMapper.select(pro).get(0);

        if (p.getKahunaId()==project.getKahunaId()){
            projectMapper.delete(project.getId());
            return Result.success();
        }
        return Result.error("你不是该项目的负责人，无法删除");
    }
}
